// pages/home/home.js
let {getHomeData,recommendBlank,homeHisearch} = require("../../api/home")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperHeight:200,
      imgs:[
        "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
        "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d80ff6ad33796a3c5eb7434fe5fb00a0.jpg?f=webp&w=1080&h=540&bg=F5EEE6",
        "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
        "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
        "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
      ],
      current:0,
      location:[
        {
          id:111,
          checked:false,
          save:"广东",
          slit:"广州市从化区鳌头镇",
          name:"唐方泽",
          phone:"18211370484"
        },
        {
          id:333,
          checked:true,
          save:"广东",
          slit:"广州市从化区鳌头镇",
          name:"唐方泽",
          phone:"18211370484"
        },
      ],
      category:[
        {
          id:9,
          img:"/image/01_03.gif",
          text:"小米上新"
        },
        {
          id:0,
          img:"/image/01_05.gif",
          text:"手机"
        },
        {
          id:0,
          img:"/image/01_07.gif",
          text:"k50系列"
        },
        {
          id:2,
          img:"/image/01_09.gif",
          text:"电视"
        },
        {
          id:3,
          img:"/image/01_11.gif",
          text:"大家电"
        },
        {
          id:6,
          img:"/image/01_18.gif",
          text:"米金商城"
        },
        {
          id:10,
          img:"/image/01_19.gif",
          text:"小米众筹"
        },
        {
          id:1,
          img:"/image/01_20.gif",
          text:"电脑"
        },
        {
          id:4,
          img:"/image/01_21.gif",
          text:"生活电器"
        },
        {
          id:5,
          img:"/image/01_22.png",
          text:"智能家居"
        },
      ],
      conIndex:0,
      swiper_height:"200px",
      goodsLists:[
        {
          name:"精选",
          title:"商品推荐",
          lists:[
            // {
                // id:"",
            //   img:"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d9d7f31258a1f84a277918cecab14aaa.jpg?f=webp&w=516&h=420",
            //   name:"小米手机",
            //   price:29,
            //   priceOld:129
            // }
          ]
        },
        {
          name:"手机",
          title:"探索黑科技",
          lists:[]
        },
        {
          name:"电视",
          title:"数码电器",
          lists:[] 
        },
      ]
  },

  // 商品 选项卡切换
  setConIndex(event){
    console.log(event.currentTarget.dataset.id);
    let index = event.currentTarget.dataset.id
    this.setData({
      conIndex:index,
    })

    this.getNavData(index);
  },

  // 分类跳转
  classUrl(event){
    let id = event.currentTarget.dataset.id
    wx.setStorageSync('classId', id)
    wx.switchTab({
      url: "/pages/category/category",
   })
  },

  // 轮播图商品切换
  setSwiperGoodsIndex(event){
    this.autoHeight();

    console.log("触发效果",event.detail.current);
    this.setData({
      conIndex:event.detail.current,
    })
  },

  // 获取 手机 家电 数据
  getNavData(index){
    if(this.data.goodsLists[index].lists.length==0){ //没有数据才请求数据
      homeHisearch({query:this.data.goodsLists[index].name}).then((data)=>{
        console.log(data);
        let list_v2 = data.data.list_v2;
        let listsArr = [];
        list_v2.forEach(item=>{
            listsArr.push({
              id:item.body.product_id,
              img:item.body.image,
              name:item.body.name,
              price:item.body.price,
              priceOld:item.body.market_price
          })
        })
        console.log("数据列表==>",listsArr);
        this.setData({
          ["goodsLists["+index+"].lists"]:listsArr
        })


      }).then(()=>{
        this.autoHeight();
      })
    }


    
  },

  // 计算内容高度
  autoHeight() {

    let {conIndex} = this.data;
    console.log(this.data);
 
      wx.createSelectorQuery()
        .select('#end' + conIndex).boundingClientRect()
        .select('#start' + conIndex).boundingClientRect().exec(rect => {
          console.log(rect);
          let _space = rect[0].top - rect[1].top ;//end高度-start高度=差值
          _space =  _space + 'px';
 
          this.setData({
            swiper_height: _space
          });
 
        })
 
  },

  // 自定义方法
  // 轮播图指示点
  setSwiperIndex(event){
    console.log("触发效果",event.detail.current);
    // this.setData 修改data中数据,数据修改后更新视图
    this.setData({
      current:event.detail.current
    })
  },

  // 获取图片高度
  bindloadImg(e){
    let {height,width} = e.detail;
    // 获取硬件系统信息
    wx.getSystemInfo({
      success:(res)=>{
        this.setData({
          swiperHeight:res.windowWidth *0.95 / width * height
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    getHomeData().then(data=>{
      let sections = data.data.data.sections;
      this.setData({
        imgs:sections[0].body.items
      })
    })
    recommendBlank().then(data=>{
      console.log(data);
      let recom_list = data.data.recom_list;
      let listsArr = [];
      recom_list.forEach(item=>{
          listsArr.push({
            id:item.action.path,
            img:item.image_url,
            name:item.name,
            price:item.price,
            priceOld:item.market_price
        })
      })
      this.setData({
        "goodsLists[0].lists":listsArr
      })

    }).then(()=>{
      this.autoHeight();
      console.log("是");
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.setStorageSync('classId', 0)

    console.log("onReady");
    wx.setStorageSync('addresslists', this.data.location)

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideTabBar()
    console.log("onShow");
    this.autoHeight();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("onHide");

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})